package test;

import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class WindDataProcess {

    private static Set<String> ignoreKeys = new HashSet<>();

    static {
        ignoreKeys.add("备用");
    }

    public static void main(String[] args) throws Exception {

        File directory = new File("/Users/hym/Desktop/F1012_FJ001");
        File[] files = directory.listFiles();
        if (files == null) {
            return;
        }

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("/Users/hym/Desktop/F1012_FJ001/F1012_FJ001.csv"), StandardCharsets.UTF_8));

        try {
            for (int no = 0; no < files.length; no++) {
                FileInputStream fileInputStream = new FileInputStream(files[no]);
                BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
                String temp;
                int n = 0;
                while ((temp = reader.readLine()) != null) {
                    if (n == 0) {
                        n++;
                        continue;
                    }
                    if (!StringUtils.isEmpty(temp)) {
                        String[] split = temp.split(",");
                        System.out.println(Arrays.toString(split));
                        //第一台分机，写csv数据标题
                        if (n == 1 && no == 0) {
                            int index = 0;
                            while (index < split.length) {
                                String s = split[index];
                                if (index == 0) {
                                    out.write("风机编号");
                                    out.write(",");
                                } else if (index != split.length - 1) {
                                    String[] kv = s.split("=");
                                    if (kv.length == 2) {
                                        String key = kv[0];
                                        if (key.contains("{")) {
                                            key = key.substring(key.indexOf("{")+1);
                                        }
                                        out.write(key);
                                        out.write(",");
                                    }

                                } else {
                                    out.write("时间");
                                    out.newLine();
                                }
                                index++;
                            }

                        }

                        for (int i = 0; i < split.length; i++) {
                            //写数据
                            if (n >= 1) {
                                String s = split[i];
                                if (i == 0) {
                                    out.write(s);
                                    out.write(",");
                                } else if (i != split.length - 1) {
                                    String[] kv = s.split("=");
                                    if (kv.length == 2) {
                                        String value = kv[1];
                                        if (value.contains("}")) {
                                            value = value.substring(0, value.indexOf("}"));
                                        }
                                        out.write(value);
                                        out.write(",");
                                    }
                                } else {
                                    out.write(s);
                                    out.newLine();
                                }
                            }
                        }
                    }
                    n++;
                }
            }
        } finally {
            out.flush();
            out.close();
        }

    }

}
